package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.home.dao.ExhibitionSiteDrawingMapper;
import com.eastfair.home.dto.ExhibitionSiteDrawingDTO;
import com.eastfair.home.entity.ExhibitionSiteDrawing;
import com.eastfair.home.service.ExhibitionSiteDrawingService;
import com.eastfair.home.service.HomeUserAccountService;
import com.eastfair.home.vo.ExhibitionSiteDrawingVO;
import com.eastfair.venueservice.vo.ServiceOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 展会展位图纸
 * </p>
 *
 * @author dqq
 * @date 2022-12-27
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ExhibitionSiteDrawingServiceImpl extends SuperServiceImpl<ExhibitionSiteDrawingMapper, ExhibitionSiteDrawing> implements ExhibitionSiteDrawingService {

    @Resource
    private HomeUserAccountService homeUserAccountService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ExhibitionSiteDrawing> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ExhibitionSiteDrawing model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public Page<ExhibitionSiteDrawingVO> listExhibitionSiteDrawingVO(PageParams<ExhibitionSiteDrawingDTO> pageParams) {
        log.info("listExhibitionSiteDrawingVO - 分页查询展会展位图纸列表, pageParams={}", pageParams);
        Page<ExhibitionSiteDrawing> pageParam = pageParams.buildPage();
        ExhibitionSiteDrawingDTO model = pageParams.getModel();
        QueryWrapper<ExhibitionSiteDrawing> queryWrapper = createExhibitionSiteDrawingQueryWrapper(model);
        Page<ExhibitionSiteDrawing> page = page(pageParam, queryWrapper);
        Page<ExhibitionSiteDrawingVO> voPage = new Page<>();
        BeanUtil.copyProperties(page, voPage);
        List<ExhibitionSiteDrawing> list = page.getRecords();
        if (list == null || list.isEmpty()) {
            return voPage;
        }
        // 查询创建人姓名
        List<Long> createByIds = list
                .stream()
                .map(ExhibitionSiteDrawing::getCreatedBy)
                .collect(Collectors.toList());
        Map<Long, String> userMap = homeUserAccountService.listUserAccount(createByIds);
        // 封装创建人姓名
        List<ExhibitionSiteDrawingVO> voList = list
                .stream()
                .map(exhibitionSiteDrawing -> {
                    ExhibitionSiteDrawingVO exhibitionSiteDrawingVO = BeanUtil.toBean(exhibitionSiteDrawing, ExhibitionSiteDrawingVO.class);
                    String username = userMap.get(exhibitionSiteDrawingVO.getCreatedBy());
                    exhibitionSiteDrawingVO.setCreatedByName(username);
                    return exhibitionSiteDrawingVO;
                })
                .collect(Collectors.toList());
        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    public List<ExhibitionSiteDrawing> listExhibitionSiteDrawing(ExhibitionSiteDrawingDTO model) {
        QueryWrapper<ExhibitionSiteDrawing> queryWrapper = createExhibitionSiteDrawingQueryWrapper(model);
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveExhibitionSiteDrawing(ExhibitionSiteDrawingDTO exhibitionSiteDrawingDTO) {
        log.info("saveExhibitionSiteDrawing - 保存展会展位图纸, exhibitionSiteDrawingDTO={}", exhibitionSiteDrawingDTO);
        ExhibitionSiteDrawing exhibitionSiteDrawing = BeanUtil.toBean(exhibitionSiteDrawingDTO, ExhibitionSiteDrawing.class);
        return save(exhibitionSiteDrawing);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<ExhibitionSiteDrawingDTO> list) {
        log.info("saveBatch - 批量保存, list={}", list);
        if (list == null || list.isEmpty()) {
            return false;
        }
        list.forEach(this::saveExhibitionSiteDrawing);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBySiteName(ExhibitionSiteDrawingDTO exhibitionSiteDrawingDTO) {
        List<ExhibitionSiteDrawing> list = listExhibitionSiteDrawing(exhibitionSiteDrawingDTO);
        if (list == null || list.isEmpty()) {
            return false;
        }
        return removeByIdsOfLogic(list);
    }

    private QueryWrapper<ExhibitionSiteDrawing> createExhibitionSiteDrawingQueryWrapper(ExhibitionSiteDrawingDTO model) {
        QueryWrapper<ExhibitionSiteDrawing> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(ExhibitionSiteDrawing::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(model.getExhibitionManageId() != null, ExhibitionSiteDrawing::getExhibitionManageId, model.getExhibitionManageId())
                .eq(StrUtil.isNotBlank(model.getSpaceCode()), ExhibitionSiteDrawing::getSpaceCode, model.getSpaceCode())
                .eq(StrUtil.isNotBlank(model.getSiteName()), ExhibitionSiteDrawing::getSiteName, model.getSiteName())
        ;
        return queryWrapper;
    }
}
